package com.young.common.context;

import org.springframework.util.StringUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 2:14 PM
 */
public final class SelectorLocalizer {
    private static final Pattern PATTERN = Pattern.compile("_selector_\\.([a-z]+)\\.([a-z]+)\\.(\\S+)");

    public SelectorLocalizer() {
    }

    public static Map<String, Object> localize(Properties properties, String profile, String cloud) {
        Map<String, Object> container = new HashMap();
        Map<String, Map<String, Map<String, Object>>> selector = new HashMap();
        Set<String> keys = properties.stringPropertyNames();
        Iterator var6 = keys.iterator();

        while(var6.hasNext()) {
            String key = (String)var6.next();
            if (!key.startsWith("_selector_")) {
                container.put(key, properties.getProperty(key));
            } else {
                Matcher matcher = PATTERN.matcher(key);
                if (matcher.find() && matcher.groupCount() == 3) {
                    String property = matcher.group(3);
                    if (!StringUtils.isEmpty(property)) {
                        Map<String, Map<String, Object>> pm = (Map)selector.getOrDefault(property, new HashMap());
                        selector.putIfAbsent(property, pm);
                        String _cloud = matcher.group(2);
                        Map<String, Object> cm = (Map)pm.getOrDefault(_cloud, new HashMap());
                        pm.putIfAbsent(_cloud, cm);
                        String _profile = matcher.group(1);
                        cm.put(_profile, properties.getProperty(key));
                    }
                }
            }
        }

        container.putAll(selectorPropertiesNative(selector, profile, cloud));
        return container;
    }

    private static Map<String, Object> selectorPropertiesNative(Map<String, Map<String, Map<String, Object>>> selector, String profile, String cloud) {
        Map<String, Object> container = new HashMap();
        Set<String> keys = selector.keySet();
        Iterator var5 = keys.iterator();

        while(true) {
            while(var5.hasNext()) {
                String key = (String)var5.next();
                Map<String, Map<String, Object>> mapper = (Map)selector.get(key);
                Map<String, Object> cm = (Map)mapper.get(cloud);
                if (cm != null && cm.containsKey(profile)) {
                    container.put(key, cm.get(profile));
                } else {
                    cm = (Map)mapper.get(Cloud.DEFAULT.code);
                    if (cm != null) {
                        String _profile = cm.containsKey(profile) ? profile : Profile.DEFAULT.getStandard();
                        container.put(key, cm.get(_profile));
                    } else {
                        container.put(key, (Object)null);
                    }
                }
            }

            return container;
        }
    }
}

